﻿using Ftcb.Common;
using Kanban.DALContainer;
using Kanban.IBLL;
using Kanban.IDal;
using Kanban.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kanban.BLL
{
    public class Speed_SettingService : BaseService<Speed_Setting>, ISpeed_SettingService
    {
        private readonly ISpeed_SettingDal Speed_SettingDal = Container.Resolve<ISpeed_SettingDal>();

        public override void SetCurrentDal()
        {
            Dal = Speed_SettingDal;
        }

        public async Task<Tuple<IList<object>, int>> GetModelsByPageAsync(string search, int pageSize, int pageIndex)
        {
            return await Speed_SettingDal.GetModelsByPageAsync(search, pageSize, pageIndex);
        }

        public async Task<bool> Save(Speed_Setting model)
        {
            var result = true;
            var query = await GetModelAsync(model.Id);
            if (query == null)
            {
                result = await AddAsync(model) > 0;
            }
            else
            {
                query.Value = model.Value;
                query.Type = model.Type;
                query.IsValid = model.IsValid;
                result = await UpdateAsync(query) > 0;
            }
            await SetCache();
            return result;
        }

        public async Task SetCache()
        {
            string key = "GetCache_SpeedSetting";
            var list = (await Speed_SettingDal.GetListAsync("1=1")).ToList();
            CacheHelper.SetCache(key, list);
        }
    }
}
